﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：王明亮
 * 创建时间：2023/8/4 14:58:31
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace MyWebApp.Helper
{
    /// <summary>
    /// JWT帮助类
    /// </summary>
    public class JwtHelper
    {
        //私钥appsettings.json中配置
        private static string secret = Helper.ConfigHelper.GetSectionValue("TokenSecret");

        /// <summary>
        /// 生成JwtToken
        /// </summary>
        /// <param name="payload">不敏感的用户数据</param>
        /// <returns></returns>
        public static string SetJwtEncode(Dictionary<string, object> payload)
        {
            IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
            IJsonSerializer serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);

            var token = encoder.Encode(payload, secret);
            return token;
        }

        /// <summary>
        /// 根据jwtToken获取实体
        /// </summary>
        /// <param name="token">jwtToken</param>
        /// <returns></returns>
        public static LoginUserInfo GetJwtDecode(string token)
        {
            LoginUserInfo loginUserInfo = new LoginUserInfo();
            try
            {
               
                IJsonSerializer serializer = new JsonNetSerializer();
                IDateTimeProvider provider = new UtcDateTimeProvider();
                IJwtValidator validator = new JwtValidator(serializer, provider);
                IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
                var algorithm = new HMACSHA256Algorithm();
                IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);
                loginUserInfo = decoder.DecodeToObject<LoginUserInfo>(token, secret, verify: true);//token为之前生成的字符串
                return loginUserInfo;
            }
            catch (System.Exception ex)
            {
                return loginUserInfo;
            }
           
        }

        /// <summary>
        /// 登录人员信息
        /// </summary>
        public class LoginUserInfo
        {
            /// <summary>
            /// 用户名
            /// </summary>
            public string username { get; set; }
            /// <summary>
            /// 密码
            /// </summary>
            public string pwd { get; set; }
            /// <summary>
            /// Token 过期时间
            /// </summary>
            public double exp { get; set; }
        }

        /// <summary>
        ///获取返回Token信息
        /// </summary>
        public class TokenResult
        {
            /// <summary>
            /// 是否成功
            /// </summary>
            public bool success { get; set; }
            /// <summary>
            /// Token字符串
            /// </summary>
            public string token { get; set; }
            /// <summary>
            /// 提示信息
            /// </summary>
            public string message { get; set; }
        }
    }
}
